package com.design.patterns.list.iterator.concreteiterator;

import com.design.patterns.list.iterator.aggregate.Department;

import java.util.Iterator;

/**
 * @author Wangzhiwen
 */
public class ComputerCollegeIterator implements Iterator<Department> {

    // 存储的数据结构
    private Department[] departments;
    // 索引
    private int position;

    public ComputerCollegeIterator(Department[] departments) {
        this.departments = departments;
    }

    // 判断下一个是否有元素
    @Override
    public boolean hasNext() {
        return position < departments.length && departments[position] != null;
    }

    // 获取下一个元素
    @Override
    public Department next() {
        Department department = departments[position];
        position++;
        return department;
    }

    // 移除一个元素
    @Override
    public void remove() {

    }

    // 实际应用中还需要判断position是越界，以及动态扩容的问题。可参考ArrayList的rangCheck和ensureCapacityInternal
}
